﻿using CGT.GOP.App_Start;
using CGT.GOP.Common;
using CGT.GOP.Models;
using CGT.Infrastructure.Dto;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Enums;
using CGT.Infrastructure.Service;
using Dapper.Extensions;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CGT.GOP.Controllers
{
    public class OrderController : BaseController
    {
        NetworkOrderService orderService = new NetworkOrderService();
        MasterBizTypeService mbService = new MasterBizTypeService();
        BizTypeService btService = new BizTypeService();
        GovLogOperateService loService = new GovLogOperateService();
        GovLogSafeService lsService = new GovLogSafeService();
        NetworkOrderRejectDicService nrService = new NetworkOrderRejectDicService();
        NetworkOrderAuditService auditService = new NetworkOrderAuditService();

        private int OrderCode = 10301;//订单管理编码
        private int RejectCode = 10108;//订单驳回字典管理编码

        #region 订单信息
        // GET: Order
        [Authentication(MenuCode = 10301)]
        public ActionResult Index()
        {
            loService.Info(CurrentUser.UserId, "访问页面", 4, "", "访问订单管理列表页面", OrderCode);
            lsService.Info(CurrentUser.UserId, "访问页面", 4, "", "访问订单管理列表页面", OrderCode);
            return View();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetOrderList()
        {
            ResultData<List<NetworkOrderDto>> rData = new ResultData<List<NetworkOrderDto>>();
            GovNetworkOrderCondition searchObj = new GovNetworkOrderCondition();
            searchObj.StartDateTime = "";
            searchObj.EndDateTime = "";
            searchObj.PageIndex = GetPageIndex();
            searchObj.PageSize = GetPageSize();
            searchObj.OpUserID = CurrentUser.UserId;
            int provinceId = EConvert.ToInt(GetParam("provinceid"));
            int cityId = EConvert.ToInt(GetParam("cityid"));
            int masterBizTypeId = EConvert.ToInt(GetParam("masterbiztype"));
            int bizTypeId = EConvert.ToInt(GetParam("biztype"));
            long orderId = EConvert.ToLong(GetParam("orderid"));
            string sDate = GetParam("sDate");
            string eDate = GetParam("eDate");
            if (provinceId > 0)
            {
                searchObj.ProvinceId = provinceId;
            }
            if (cityId > 0)
            {
                searchObj.CityId = cityId;
            }
            if (masterBizTypeId > 0)
            {
                searchObj.MasterBizTypeId = masterBizTypeId;
            }
            if (bizTypeId > 0)
            {
                searchObj.BizTypeId = bizTypeId;
            }
            if (orderId > 0)
            {
                searchObj.OrderID = orderId;
            }
            if (!string.IsNullOrWhiteSpace(sDate))
            {
                searchObj.StartDateTime = sDate;
            }
            if (!string.IsNullOrWhiteSpace(eDate))
            {
                searchObj.EndDateTime = eDate;
            }
            var list = orderService.GetList(searchObj);
            rData.data = list;
            rData.message = searchObj.OutCount.ToString();
            return ReturnResult(rData);
        }

        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authentication(MenuCode = 10301)]
        public ActionResult OrderInfo(long orderId)
        {
            var info = orderService.GetOrderInfo(orderId);
            if (info == null)
            {
                info = new NetworkOrderDetailDto();
            }
            return View(info);
        }

        /// <summary>
        /// 照片查看
        /// </summary>
        /// <param name="imgUrl"></param>
        /// <returns></returns>
        public ActionResult ImagePreview(string imgUrl)
        {
            @ViewBag.ImgUrl = imgUrl;
            return View();
        }
        #endregion

        #region 订单查询条件
        /// <summary>
        /// 一级分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetMasterBizTypeList()
        {
            ResultData<List<MasterBizType>> rData = new ResultData<List<MasterBizType>>();
            var list = mbService.QueryAll();
            rData.data = list.Where(a => a.IsActive == 1).ToList();
            return ReturnResult(rData);
        }

        /// <summary>
        /// 根据主分类ID获取二级分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult GetBizTypeListByMasterId(int masterId)
        {
            ResultData<List<BizType>> rData = new ResultData<List<BizType>>();
            var list = btService.GetBizTypeListByMasterId(masterId);
            rData.data = list;
            return ReturnResult(rData);
        }
        #endregion

        #region 订单驳回字典
        /// <summary>
        /// 字典列表
        /// </summary>
        /// <returns></returns>
        [Authentication(MenuCode =10108)]
        public ActionResult DicOrderReject()
        {
            loService.Info(CurrentUser.UserId, "访问页面", 4, "", "访问驳回字典列表页面", RejectCode);
            lsService.Info(CurrentUser.UserId, "访问页面", 4, "", "访问驳回字典列表页面", RejectCode);
            return View();
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public ContentResult GetOrderRejectList()
        {
            int pageIndex = GetPageIndex();
            int pageSize = GetPageSize();
            int masterBizTypeId = EConvert.ToInt(GetParam("masterbiztype"));
            int bizTypeId = EConvert.ToInt(GetParam("biztype"));
            Page<NetworkOrderRejectDicDto> page = nrService.GetPages(pageIndex, pageSize, masterBizTypeId, bizTypeId);
            return ReturnResult(new ResultPageData<List<NetworkOrderRejectDicDto>>
            {
                data = page.rows,
                total = page.total
            });
        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <returns></returns>
        public ActionResult DicOrderRejectEdit()
        {
            int id = EConvert.ToInt(GetParam("id"));
            string des = string.Format("{0}添加驳回信息", CurrentUser.UserName);
            NetworkOrderRejectDicInterfaceDto dicObj = null;
            if (id > 0)
            {
                dicObj = nrService.GetInfoById(id);
               
                des = string.Format("{0}修改驳回信息{1}", CurrentUser.UserName, dicObj.RejectContent);
            }
            if (dicObj == null)
            {
                dicObj = new NetworkOrderRejectDicInterfaceDto();
            }
            loService.Info(CurrentUser.UserId, "访问页面", 4, "", des, RejectCode);
            lsService.Info(CurrentUser.UserId, "访问页面", 4, "", des, RejectCode);
            return View(dicObj);
        }

        /// <summary>
        /// 保存字典
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult SaveRejectDic(NetworkOrderRejectDicModel model)
        {
            ResultData<int> rData = new ResultData<int>();
            NetworkOrderRejectDic info = new NetworkOrderRejectDic();
            try
            {
                if (model.DicId < 1)
                {

                    info.BizTypeId = model.selBizType;
                    info.RejectContent = model.txtRejectContent;
                    object id = nrService.Insert(info);
                    loService.Info(CurrentUser.UserId, "添加驳回信息字典", 1, "添加成功,返回ID:" + id, string.Format("{0}添加驳回信息字典{1}", CurrentUser.UserName, info.RejectContent), RejectCode);
                    lsService.Info(CurrentUser.UserId, "添加驳回信息字典", 1, "添加成功,返回ID:" + id, string.Format("{0}添加驳回信息字典{1}", CurrentUser.UserName, info.RejectContent), RejectCode);

                }
                else
                {
                    info = nrService.Single(model.DicId);
                    if (info != null)
                    {
                        string des = string.Format("{0}修改信息字典,数据ID:{1},原内容BizTypeId:{2},RejectContent{3},更新内容BizTypeId:{4},RejectContent{5}", info.DicId, CurrentUser.UserName, info.BizTypeId, info.RejectContent, model.selBizType, model.txtRejectContent);
                        info.BizTypeId = model.selBizType;
                        info.RejectContent = model.txtRejectContent;
                        info.DicId = model.DicId;
                        nrService.Update(info);
                        loService.Info(CurrentUser.UserId, "修改驳回信息字典", 3, "修改成功", des, RejectCode);
                        lsService.Info(CurrentUser.UserId, "修改驳回信息字典", 3, "修改成功", des, RejectCode);
                    }
                    else
                    {
                        rData.status = -1;
                        rData.message = "没有此记录";
                        loService.Info(CurrentUser.UserId, "修改驳回信息字典", 3, "修改失败", "没有此记录" + model.DicId, RejectCode);
                        lsService.Info(CurrentUser.UserId, "修改驳回信息字典", 3, "修改失败", "没有此记录" + model.DicId, RejectCode);
                    }
                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult DelRejectDic(int id)
        {
            ResultData<bool> rData = new ResultData<bool>();
            if (id < 1)
            {
                rData.status = -1;
                rData.message = "当前数据不存在";
                return ReturnResult(rData);
            }
            try
            {
                nrService.Delete(id);
                rData.data = true;
                loService.Info(CurrentUser.UserId, "删除驳回信息字典", 2, "删除成功", string.Format("{0}删除驳回信息字典数据ID:{1}", CurrentUser.UserName, id), RejectCode);
                lsService.Info(CurrentUser.UserId, "删除驳回信息字典", 2, "删除成功", string.Format("{0}删除驳回信息字典数据ID:{1}", CurrentUser.UserName, id), RejectCode);

            }
            catch (Exception ex)
            {
                rData.status = -1;
                rData.message = ex.Message;
                rData.data = false;
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 获取相关业务类型的驳回原因
        /// </summary>
        /// <param name="bizId"></param>
        /// <returns></returns>
        public ContentResult GetRejectDicByBizId(int bizId)
        {
            ResultData<List<NetworkOrderRejectDic>> rData = new ResultData<List<NetworkOrderRejectDic>>();
            var list = nrService.GetListByBizId(bizId);
            rData.data = list;
            return ReturnResult(rData);
        }
        #endregion

        #region 订单审核
        /// <summary>
        /// 订单审核
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public ActionResult OrderAudit(long orderId)
        {
            @ViewBag.BizTypeId = 0;
            @ViewBag.OrderId = orderId;
            NetworkOrder orderObj = orderService.Single(orderId);
            if (orderObj!=null)
            {
                @ViewBag.BizTypeId = orderObj.BizTypeID;
            }
            return View();
        }

        /// <summary>
        /// 保存审核结果 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ContentResult SaveAudit(NetworkOrderAuditModel model)
        {
            ResultData<int> rData = new ResultData<int>();
            NetworkOrderAudit info = new NetworkOrderAudit();
            try
            {
                NetworkOrder orderObj = orderService.Single(model.hidOrderId);
                if (orderObj != null)
                {
                    if (orderObj.OrderState == (int)OrderStateEnum.WaitAudit)
                    {
                        info.AuditResult = model.rdAuditResult;
                        info.CreateUserID = CurrentUser.UserId;
                        info.CreatedDate = DateTime.Now;
                        info.OrderID = model.hidOrderId;
                        info.RejectReason = model.selReject;
                        info.Remark = model.txtRemark;
                        auditService.Insert(info);
                        //日志
                        loService.Info(CurrentUser.UserId, "订单审核", 3, "审核成功", string.Format("{0}审核,订单ID:{1},审核结果:{2},驳回原因:{3},备注:{4}", CurrentUser.UserName, info.OrderID, info.AuditResult, info.RejectReason, info.Remark), OrderCode);
                        lsService.Info(CurrentUser.UserId, "订单审核", 3, "审核成功", string.Format("{0}审核,订单ID:{1},审核结果:{2},驳回原因:{3},备注:{4}", CurrentUser.UserName, info.OrderID, info.AuditResult, info.RejectReason, info.Remark), OrderCode);

                        orderObj.OrderState = info.AuditResult == 1 ? (int)OrderStateEnum.WaitPayment : (int)OrderStateEnum.Reject;
                        orderObj.UpdatedDate = DateTime.Now;
                        orderService.Update(orderObj);
                        string oState = EnumHelper.GetEnumDesc(typeof(OrderStateEnum), orderObj.OrderState);
                        //日志
                        loService.Info(CurrentUser.UserId, "订单审核", 3, "审核成功", string.Format("{0}审核,订单ID:{1},订单状态:{2},审核结果:{3},驳回原因:{4},备注:{5}", CurrentUser.UserName, info.OrderID, oState, info.AuditResult, info.RejectReason, info.Remark), OrderCode);
                        lsService.Info(CurrentUser.UserId, "订单审核", 3, "审核成功", string.Format("{0}审核,订单ID:{1},订单状态:{2},审核结果:{3},驳回原因:{4},备注:{5}", CurrentUser.UserName, info.OrderID, oState, info.AuditResult, info.RejectReason, info.Remark), OrderCode);
                    }
                    else
                    {
                        rData.status = -1;
                        rData.message = "非法操作";
                        loService.Info(CurrentUser.UserId, "订单审核", 3, "非法操作", string.Format("{0}审核,订单ID:{1},审核结果:{2},驳回原因:{3},备注:{4}", CurrentUser.UserName, info.OrderID, info.AuditResult, info.RejectReason, info.Remark), OrderCode);
                        lsService.Info(CurrentUser.UserId, "订单审核", 3, "非法操作", string.Format("{0}审核,订单ID:{1},审核结果:{2},驳回原因:{3},备注:{4}", CurrentUser.UserName, info.OrderID, info.AuditResult, info.RejectReason, info.Remark), OrderCode);

                    }
                }
                else
                {
                    rData.status = -1;
                    rData.message = "非法操作";
                    loService.Info(CurrentUser.UserId, "订单审核", 3, "非法操作", string.Format("{0}审核,订单ID:{1},审核结果:{2},驳回原因:{3},备注:{4}", CurrentUser.UserName, info.OrderID, info.AuditResult, info.RejectReason, info.Remark), OrderCode);
                    lsService.Info(CurrentUser.UserId, "订单审核", 3, "非法操作", string.Format("{0}审核,订单ID:{1},审核结果:{2},驳回原因:{3},备注:{4}", CurrentUser.UserName, info.OrderID, info.AuditResult, info.RejectReason, info.Remark), OrderCode);

                }
            }
            catch (Exception ex)
            {
                rData.status = -1;
                rData.message = ex.Message;
            }
            return ReturnResult(rData);
        }

        /// <summary>
        /// 订单审核结果
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public ActionResult OrderAuditResult(long orderId)
        {
            @ViewBag.OrderId = orderId;
            @ViewBag.OrderCreateTime = "";
            NetworkOrder orderObj = orderService.Single(orderId);
            if (orderObj!=null)
            {
                @ViewBag.OrderCreateTime = orderObj.CreatedDate.ToString("yyyy-MM-dd HH:mm:ss");
            }
            return View();
        }

        /// <summary>
        /// 获取操作记录
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public ContentResult GetOperateRecordByOrderId(long orderId)
        {
            ResultData<List<NetworkOrderAuditDto>> rData = new ResultData<List<NetworkOrderAuditDto>>();
            var list = auditService.GetListByOrderId(orderId);
            rData.data = list;
            return ReturnResult(rData);
        }
        #endregion
    }
}